/*
 * Copyright 2011-2012 Rivoli team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.rivoli.builder.configuration

import com.googlecode.rivoli.RivoliException
import com.googlecode.rivoli.configuration.Configurable
import com.googlecode.rivoli.configuration.Configuration

import org.testng.annotations.Test

class ConfigurationBuilderTest {
	@Test
	void 'configuration should be buildable'() {
		assert ConfigurationBuilder.build {}
	}

	@Test
	void 'configurables should be addable'() {
		Configuration configuration = ConfigurationBuilder.build {
			internet(InternetConfiguration)
		}

		assert configuration.getConfigurable('internet') in InternetConfiguration
	}

	@Test
	void 'configurables should be parameterisable'() {
		Configuration configuration = ConfigurationBuilder.build {
			'fast internet'(InternetConfiguration, speed: 'fast')
			'slow internet'(InternetConfiguration, speed: 'slow')
		}

		assert configuration.getConfigurable('fast internet').speed == 'fast'
		assert configuration.getConfigurable('slow internet').speed == 'slow'
	}

	@Test(expectedExceptions = RivoliException)
	void 'adding configurable without type should not be allowed'() {
		ConfigurationBuilder.build {
			'nothing at all'()
		}
	}

	@Test(expectedExceptions = RivoliException)
	void 'adding non-configurable should not be allowed'() {
		ConfigurationBuilder.build {
			list(ArrayList)
		}
	}

	private class InternetConfiguration implements Configurable {
		String speed

		void init(Map<String, ?> parameters) {
			speed = parameters['speed']
		}
	}
}